<template>
  <a-modal
    :title="title"
    :width="1400"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :footer="null"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-card :bordered="false">
      
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :showPagination="false">

        <span slot="action" slot-scope="text, record">
          <a @click="$refs.packingDetailModal.show(record)">出库记录</a>
          <a-divider type="vertical"/>
        <a @click="$refs.backStoreRecordModal.show(record)">退库记录</a>
        </span>
      
      </s-table>
      
      <packing-detail-modal ref="packingDetailModal"/>
      <back-store-record-modal ref="backStoreRecordModal"/>
    </a-card>
  </a-modal>
</template>

<script>
import {STable} from '@/components'
import {page} from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'

import packingDetailModal from './packingDetailModal'
import BackStoreRecordModal from "./backStoreRecordModal.vue";

export default {
  components: {
    BackStoreRecordModal,
    STable,
    packingDetailModal,
  },
  data() {
    return {
      title: null,
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},  // 查询参数
      viewState: '',
      columns: [
        {
          title: '序号', width: '80px',
          dataIndex: '',
          customRender: (text, record, index) => `${index + 1}`
        },
        {
          title: '包材编号',
          dataIndex: 'materialCode'
        },
        {
          title: '包材名称',
          dataIndex: 'materialName',
        },
        {
          title: '需求数量',
          dataIndex: 'number'
        },
        {
          title: '已出库数量',
          dataIndex: 'completedNumber'
        }, 
				{
          title: '领用方',
          dataIndex: 'classGroupName'
        },
				{
          title: '待确认退库数量',
          dataIndex: 'returnNumber'
        },
        {
          title: '待出库数量',
          dataIndex: '',
          customRender: (text, record, index) => `${record.number - record.completedNumber}`
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.queryParam.orderId = this.emsOrder.orderId
        this.queryParam.pageSize = -1
        this.queryParam.materialType = this.$businessEnum.MATERIAL_TYPE_ENUM.packing.id
        return page(Object.assign(parameter, this.queryParam)).then((res) => {
          res.data.rows.forEach(value => {
            value.normalNumber = 0
          })
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      defaultExpandedKeys: [],
      replaceFields: {
        key: 'id',
      },
      visible: false,
      confirmLoading: false,
      emsOrder: null,
    }
  },
  methods: {
    show(record) {
      this.visible = true
      this.emsOrder = record
      this.title = `包材列表（${record.orderNumber}）`
      setTimeout(() => {
        this.handleOk()
      }, 100)
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleSubmit() {
    },
    handleCancel() {
      this.visible = false
    },
  }
}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
